#include <reg52.h>
#include <intrins.h>
#define uchar unsigned char
#define uint unsigned int

uchar str[4];
uchar i = 0;
uchar ready = 0;
uchar busy = 0;

void UartInit(void) //��ʼ������
{
	SCON = 0x50; //���ڷ�ʽ1, 8-n-1, ��������.
	TMOD = 0x20; //T1��ʽ2
	TH1 = 0xFD;
	TL1 = 0xFD;
	TR1 = 1;
	ES = 1; //���ж�.
	EA = 1;
}
void sendData(uchar d)
{
	while (busy)
		; //Wait for the completion of the previous data is sent
	busy = 1;
	SBUF = d;
}
void SendString(uchar *s)
{
	while (*s)
	{
		sendData(*s++);
	}
}
main()
{
	UartInit();
	SendString("test is begin~\r\n");
	while (1)
	{
		if (ready)
		{
			ready = 0;
			SendString(str);
		}
	}
}

void uart(void) interrupt 4
{
	uchar temp;
	if (RI)
	{
		RI = 0;
		// buffer[wptr++] = SBUF;
		// wptr &= 0x0f;//这种更好
		temp = SBUF;
		str[i] = temp;
		i++;
		if (i == 3)
		{
			i = 0;
			ready = 1;
		}
	}
	if (TI)
	{
		TI = 0;
		busy = 0;
	}
}